<?php

class Bills extends Eloquent {
    protected $table = 'bills';
    
    public static function ListBill($shop_id)
    {        
        try{
         
            $select =  DB::table('bills')
                ->join('bill_detail','bill_detail.bill_id', '=', 'bills.id')
                ->join('users', 'bills.user_id', '=', 'users.id')
                ->join('customers','users.shop_id' , '=','customers.shop_id' )
                ->select('bills.*', 'bill_detail.*', 'customers.name','users.name')
                ->where('users.shop_id', '=', $shop_id);
            return $select;
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
 
    public static function Insert($shop_id,$billnumber,$total_money,$description,$customer_id,$status,$user_id,$good_id,$number,$price,$tax,$discount,$total_money,$comments)
    {
        
      try{
            $users = Users::where('shop_id',$shop_id)->get()->first();
            if($users){
                $bill= new Bills();
                $bill->billnumber = $billnumber;
                $bill->total_money =$total_money;
                $bill->description=$description;
                $bill->customer_id=$customer_id;
                $bill->status=$status;
                $bill->user_id =$user_id;
                $bill->save();
                $bill_id = $bill->id;
               
               if($bill_id){
                    $billdetail= new Bill_detail();
                    $billdetail->bill_id = $bill_id;
                    $billdetail->good_id =$good_id;
                    $billdetail->number=$number;
                    $billdetail->price=$price;
                    $billdetail->tax=$tax;
                    $billdetail->discount=$discount;
                    $billdetail->total_money=$total_money;
                    $billdetail->comments=$comments;
                    $billdetail->save();
                    return $bill_id;
                }else{
                        return json_encode(array('error'=>'insert fail'));
                    }
            }else{
                return json_encode(array('error'=>'insert fail '));
            } 
            
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    
    
    public static function Edit($id,$billnumber,$total_money,$user_id,$description,$customer_id,$status,
                $good_id,$number,$price,$tax,$discount,$total_money,$comments)
    {
      try{
        
            if($bill= Bills::where('id',$id)->get()->first())
            {
                $bill->billnumber = $billnumber;
                $bill->total_money =$total_money;
                $bill->user_id=$user_id;
                $bill->description=$description;
                $bill->customer_id=$customer_id;
                $bill->status=$status;
                $bill->save();
                            
                $billdetail= Bill_detail::where('bill_id',$id)->get()->first();
                if($billdetail){
                    Bill_detail::where('bill_id',$id)->update(array(
                                                        'bill_id' => $id,
                                                        'good_id' =>$good_id,
                                                        'number'=>$number,
                                                        'price'=>$price,
                                                        'tax'=>$tax,
                                                        'discount'=>$discount,
                                                        'total_money'=>$total_money,
                                                        'comments'=>$comments
                                                    ));
                    //return json_encode(array('status'=>'success'));
                    return $billdetail;
                }else{
                    $billdetail= new Bill_detail();
                    $billdetail->bill_id = $id;
                    $billdetail->good_id =$good_id;
                    $billdetail->number=$number;
                    $billdetail->price=$price;
                    $billdetail->tax=$tax;
                    $billdetail->discount=$discount;
                    $billdetail->total_money=$total_money;
                    $billdetail->comments=$comments;
                    $billdetail->save();
                }    
            }else{
                return json_encode(array('status'=>'not edit'));
            }
          }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    
    
        
        public static function Del($id){
        try{
            $query_delete ="DELETE FROM bills WHERE id = '$id'";
            $delete=DB::delete($query_delete);
            
            $query_delete2 ="DELETE FROM bill_detail WHERE bill_id = '$id'";
            $delete2 =DB::delete($query_delete2);          
            return true;
        }
        catch(Exception $e){
            throw $e;
        }
    }    
    
    
        public static function UpStatus($id,$status) 
        {
        try{
            $query_edit =" UPDATE bills SET status='$status' WHERE id = '$id' ";
            $query= DB::update($query_edit);
            return true;
         }
        catch(Exception $e)
        {
            throw $e;
        }
        
    } 

}

?>
